// pages/ShowTicket/ShowTickeAddIdCard/index.js
var common = require("../../../public.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    phone: '',
    certificateNo: '',
    buyerId: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    let buyerInfo = {
      id: '',
      name: '',
      phone: '',
      certificateNo: ''
    }
    if (options.buyerInfo) {
      buyerInfo = JSON.parse(options.buyerInfo)

    }
    wx.getStorage({
      key: 'user',
      success: function (res) {
        
        that.setData({
          userInfo: res.data.user,
          token: res.data.token,
          openid: res.data.openid,
          buyerId: buyerInfo.id,
          name: buyerInfo.name,
          phone: buyerInfo.phone,
          certificateNo: buyerInfo.certificateNo
        })
      },
    })
  },
  onNameChange(e) {
    this.setData({
      name: e.detail
    })
  },
  onPhoneChange(e) {
    this.setData({
      phone: e.detail
    })
  },
  onCardChange(e) {
    this.setData({
      certificateNo: e.detail
    })
  },
  submit() {

    let that = this
    if (that.data.name == '') {
      wx.showToast({
        title: '请填写姓名',
        icon: 'none'
      })
      return
    }
    if (that.data.phone == '') {
      wx.showToast({
        title: '请填写手机号',
        icon: 'none'
      })
      return
    }
    if (that.data.certificateNo == '') {
      wx.showToast({
        title: '请填写身份证号',
        icon: 'none'
      })
      return
    }
    if (this.data.buyerId) { // 若有Id说明 修改
      let tempJson = {
        userId: that.data.userInfo.id,
        token: that.data.token,
        name: that.data.name,
        phone: that.data.phone,
        certificateNo: that.data.certificateNo,
        buyerId: that.data.buyerId
      }
      common.wxRequest("/scenicTicket/ticketBuyer/updateScenicTicketBuyer", "post", tempJson, 'application/x-www-form-urlencoded', function (res) {
        if (res.status == 200) {
          wx.showToast({
            title: '修改成功',
          })

          let pages = getCurrentPages();
          let beforePage = pages[pages.length - 2];
          beforePage.setData({
            selectedBuyerList: [],
            showBuyerPanel: true
          })
          beforePage.getBuyerList();
          wx.navigateBack({
            delta: 1
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      })
    } else { // 新增
      let tempJson = {
        userId: that.data.userInfo.id,
        token: that.data.token,
        name: that.data.name,
        phone: that.data.phone,
        certificateNo: that.data.certificateNo
      }
      common.wxRequest("/scenicTicket/ticketBuyer/saveScenicTicketBuyer", "post", tempJson, 'application/x-www-form-urlencoded', function (res) {
        console.log(res)
        if (res.status == 200) {
          wx.showToast({
            title: '添加成功',
          })

          let pages = getCurrentPages();
          let beforePage = pages[pages.length - 2];
          beforePage.setData({
            selectedBuyerList: [],
            showBuyerPanel: true
          })
          beforePage.getBuyerList();
          wx.navigateBack({
            delta: 1
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      })
    }


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})